#include "StdAfx.h"
#include "ProgressBarUI.h"
#include "BlueRenderEngineUI.h"

#define IDB_BITMAP_PROGRESS             135

CProgressBarUI::CProgressBarUI(void):m_hBitmap(NULL),m_nPos(0),m_nRange(399),m_pCallback(NULL),m_bIsInit(false)
{
	LoadProgressImg();
}

CProgressBarUI::~CProgressBarUI(void)
{
	if( m_hBitmap != NULL ) ::DeleteObject(m_hBitmap);
}

CStdString CProgressBarUI::GetName() const
{
	return L"progressbar";
}

void CProgressBarUI::SetCallBack(IProgressBarCallbackUI* pCallback)
{
	m_pCallback = pCallback;
}

void CProgressBarUI::LoadProgressImg()
{
	if( m_hBitmap != NULL ) ::DeleteObject(m_hBitmap);
	m_hBitmap = (HBITMAP) ::LoadImage(m_pManager->GetResourceInstance(),MAKEINTRESOURCE(IDB_BITMAP_PROGRESS), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
	ASSERT(m_hBitmap!=NULL);
	if( m_hBitmap == NULL ) return;
	::GetObject(m_hBitmap, sizeof(BITMAP), &m_BitmapInfo);
	//Invalidate();
}

void CProgressBarUI::SetRange(int nRange)
{
	m_nRange = nRange;
}

int CProgressBarUI::GetPos()
{
	if(m_pCallback)
	{
		int nPos = m_pCallback->GetProgressPos();
		return m_nRange * nPos / 100;
	}
	return 0;
}

LPCTSTR CProgressBarUI::GetClass() const
{
	return _T("ProgressBarUI");
}

SIZE CProgressBarUI::EstimateSize(SIZE)
{
	SIZE sz = {0,36};
	return sz;
}


UINT CProgressBarUI::GetControlFlags() const
{
	return UIFLAG_TABSTOP;
}

void CProgressBarUI::DoPaint(HDC hDC, const RECT& rcPaint)
{
	if(m_hBitmap)
	{
		m_rcProcess = m_rcItem;
		int nPos = GetPos();
		if(nPos <= 0)
			m_rcProcess.right = m_rcProcess.left + 0;
		else
			m_rcProcess.right = m_rcProcess.left + nPos;
		HDC memdc = ::CreateCompatibleDC(hDC);
		HBITMAP bmpTemp = (HBITMAP)::SelectObject(memdc,m_hBitmap);
		int nWidth = m_rcProcess.right - m_rcProcess.left;
		int nHeight = m_rcProcess.bottom - m_rcProcess.top;
/*#ifdef _DEBUG
		TCHAR sz[MAX_PATH];
		_itow(m_rcProcess.right,sz,10);
		_tcscat(sz,L"\n");
		OutputDebugString(L"DoPaint ");
		OutputDebugString(sz);
		RECT rc;
		rc.left = m_rcProcess.left;
		rc.right = rc.left + nPos;
		rc.top = m_rcProcess.top;
		rc.bottom = m_rcProcess.bottom;
		HBRUSH hBrush = ::CreateSolidBrush( RGB( 0, 0, 0 ) );
		::FillRect(hDC, &rc, hBrush ); 
		::DeleteObject( hBrush );
#endif*/
		//if(m_nRange == nPos)
			//OutputDebugString(L"\nfjkdsjfkldsjfkldjfklsd\n");
		//::StretchBlt(hDC,m_rcItem.left,m_rcItem.top,nWidth,nHeight, 
			//memdc, 0, 0,m_BitmapInfo.bmWidth, m_BitmapInfo.bmHeight, SRCCOPY);
		::BitBlt(hDC,m_rcItem.left,m_rcItem.top,nWidth,nHeight, 
			memdc, 0, 0, SRCCOPY);
		::SelectObject(memdc, bmpTemp);
		::DeleteDC(memdc);
	}
}

void CProgressBarUI::SetPos(RECT rc)
{
	//if(!m_bIsInit)
	//{
		//m_bIsInit = true;
		//m_rcItem = rc;
	//}
	m_rcItem.left =  119;
	m_rcItem.top = 34;
	m_rcItem.right = 518;
	m_rcItem.bottom = 56;

	Invalidate();
}

